package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Upload;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //       0，乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        Upload.imgUpload(req);

        ProductModel productModel = new ProductModel();
        String cookTypeId = (String) req.getAttribute("cookTypeId");
        productModel.setCookTypeId(Integer.parseInt(cookTypeId));
        productModel.setName((String) req.getAttribute("productName"));
        productModel.setMarketPrice(Double.parseDouble((String) req.getAttribute("productMarketPrice")));
        productModel.setNomalPrice(Double.parseDouble((String) req.getAttribute("productNomalPrice")));
        productModel.setDiscrib((String) req.getAttribute("productDiscrib"));
        productModel.setImgHref((String) req.getAttribute("productImgHref"));
        productModel.setTitle((String) req.getAttribute("productTitle"));

        IProductService iProductService = new ProductServiceImpl();
        int result = iProductService.add(productModel);
//        4.得到返回结果
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);

    }
}
